package com.bookstore.Controller;

import com.bookstore.Domain.Code;
import com.bookstore.Domain.Person;
import com.bookstore.Domain.Result;
import com.bookstore.Service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 未来
 */
@CrossOrigin
@Controller
@RequestMapping("/persons")
public class PersonController {
    private final PersonService personService;

    @Autowired
    public PersonController(PersonService personService) {
        this.personService = personService;
    }

    @GetMapping
    @ResponseBody
    public Result getAll() {
        List<Person> allPerson = personService.getAllPerson();
        Integer code = allPerson != null ? Code.GET_OK : Code.GET_ERR;
        String msg = allPerson != null ? Code.MSG_OK : Code.MSG_ERR;
        return new Result(code, allPerson, msg);
    }

    @PostMapping
    @ResponseBody
    public Result add(@RequestBody Person person) {//@RequestBody:注解对应实体类属性，内部使用set方法设值
        boolean flag = personService.addPerson(person);
        return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);
    }

    @DeleteMapping
    @ResponseBody
    public Result deleteBook(@RequestBody int[] ids) {
        boolean flag = personService.deletePesson(ids);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }
}
